<?
$r = "../";
require_once($r."_engine/header_nocache.php");
require_once($r."_engine/functions.php");
require_once($r."_engine/c_email_template.php");
require_once($r."_engine/c_sp_gate.php");
	
if (@$todo == "update" || @$todo == "add"|| @$todo == "send_test")
{
	// Convert form data to DB representation

	// List of fields to be update in the database
	get_form_values($values, array(
		"template_name",
		"email_from", "email_to", "email_cc", "email_bcc",
		"email_subject", "template_text",
	));

	$te_em_tpl = new CEmailTemplate();

	if (@$todo == "update" || @$todo == "send_test") 
	{
		$te_em_tpl->id = $id;
		$te_em_tpl->Update($values);
	}
	else
	{
		$te_em_tpl->Insert($values);
	}
    
    if (@$todo == "send_test")
    {
        foreach ($g_alert_format_map as $k => $v)
        {
        	// Send email to user with password
        	$g_spg->SendEmail(array(
                "email_content_type"    => $k,
                "email_from"            => $f_email_from,
                "email_to"              => $f_email_to,
                "email_cc"              => $f_email_cc,
                "email_bcc"             => $f_email_bcc,
                "email_subject"         => $f_email_subject,
                "email_template"        => $f_template_text,
        	));
        }
    
    	generate_html_redirect("emails_templates_edit.php?id=".@$id);
    }
}
else if (@$todo == "remove")
{
	$g_st_ets->Delete(@$f_ids);
}
else 
{
	var_dump($todo);
	die("Incorrect data - todo.");
}

generate_html_redirect("emails_templates.php");
?>